package com.equipment.management.service.impl;

import com.equipment.management.entity.User;
import com.equipment.management.dao.UserDao;
import com.equipment.management.service.UserService;
import com.equipment.management.util.Base64Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Slf4j
@Service(value = "userService")
public class UserServiceImpl extends Base64Util implements UserService {

    @Autowired
    private UserDao userDao;//这里会报错，但是并不会影响

    /*
    * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
    * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
    * pageNum 开始页数
    * pageSize 每页显示的数据条数
    * */
    @Override
    public PageInfo<Map> findAllUser(int pageNum, int pageSize, String name) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<Map> userDomains = userDao.selectUsers(name);
        PageInfo result = new PageInfo(userDomains);
        return result;
    }


    @Override
    public User getUserByAccount(String account) {

        User us = userDao.getUserByAccount(account);
        return us;
    }

    @Override
    public User getUserByMessage(String account, String password) throws Exception {

        User us = userDao.getUserByMessage(account, encrypt(password));
        return us;
    }


    @Override
    public int addUser(User user) throws Exception {
        user.setState(1);//1=true,0=false
        user.setCreate_time(new Date());
        user.setPassword(encrypt(user.getPassword()));
        log.info("会员参数：{}",user);
        return userDao.addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        int i = userDao.deleteUser(id);
        return i;
    }


}
